//
// Created by ASUS on 2024/7/3/星期三.
//
#include <bits/stdc++.h>
using namespace std;

int diameter(vector<vector<int>>& edges) {
	vector<vector<int>> g(edges.size() + 1);
	for (auto& e : edges) {
		int x = e[0], y = e[1];
		g[x].push_back(y);
		g[y].push_back(x);
	}

	int res = 0;
	auto dfs = [&](auto&& dfs, int x, int fa) -> int {
		int max_len = 0;
		for (auto y : g[x]) {
			if (y != fa) {
				int sub_len = dfs(dfs, y, x) + 1;
				res = max(res, max_len + sub_len);
				max_len = max(max_len, sub_len);
			}
		}
		return max_len;
	};
	dfs(dfs, 0, -1);
	return res;
}